/**
 * Data access object for users of LSCS
 */
package ca.lscs.dal;

import java.util.List;

import ca.lscs.model.AppUser;
import ca.lscs.model.UserType;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.helper.DAOBase;

/**
 * @author Steve Pennington
 *
 */
public class UserDao extends DAOBase {
	
	private static UserDao instance = new UserDao();
	
	private UserDao() {
		ObjectifyService.register(AppUser.class);
	}
	
	public static UserDao getInstance() {
		return instance;
	}
	
	public Key<AppUser> put(AppUser user) throws DuplicateUsernameException {
		int count = ofy().query(AppUser.class).filter("username", user.getUsername()).count();
		if(count > 0) {
			throw new DuplicateUsernameException();
		}
		return ofy().put(user);
	}

	public AppUser login(String username, String password) {
		return ofy().query(AppUser.class).filter("username", username).filter("password", password).get();
	}
	
	public List<AppUser> getByType(UserType type) {
		return ofy().query(AppUser.class).filter("type", type).list();
	}
	
	public AppUser get(Key<AppUser> id) {
		return ofy().find(id);
	}
}
